package com.pigs.springbootpigsmongodbcomment.controller;

import com.mongodb.client.result.DeleteResult;
import com.pigs.springbootpigsmongodbcomment.entity.CommentEntity;
import com.pigs.springbootpigsmongodbcomment.model.ResultFormat;
import com.pigs.springbootpigsmongodbcomment.service.CommentService;
import com.pigs.springbootpigsmongodbcomment.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author PIGS
 * @version 1.0
 * @date 2020/4/23 19:50
 * @effect :
 * 评论控制器
 */
@RestController
@RequestMapping("/comment/")
public class CommentContoller {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     */
    @PostMapping("saveComment")
    public ResultFormat saveComment(@RequestBody CommentEntity commentEntity) {
        System.out.println(commentEntity);
        return ResultUtil.success(commentService.saveComment(commentEntity));
    }


    /**
     * 查询评论
     *
     * @return
     */
    @GetMapping("queryCommentAll")
    public ResultFormat queryCommentAll() {
        List<CommentEntity> commentEntities = commentService.queryComments();
        return ResultUtil.success(commentEntities);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("delComment")
    public ResultFormat delComment(Long userId) {
        System.out.println(userId);
        DeleteResult deleteResult = commentService.delComment(userId);
        System.out.println(deleteResult);
        return ResultUtil.success(deleteResult.getDeletedCount());
    }


    /**
     * 点赞评论
     */
    @PutMapping("clickLine")
    public ResultFormat clickLine(Long userId) {
        CommentEntity commentEntity = commentService.clickLine(userId);
        if (commentEntity == null) {
            return ResultUtil.error(100,"点赞失败");
        }

        return ResultUtil.success();
    }

}
